//
//  HealthTimeSetViewController.m
//  project
//
//  Created by 圆圆陈 on 2018/3/17.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "HealthTimeSetViewController.h"

@interface HealthTimeSetViewController ()
@property(nonatomic, strong) NSMutableArray *timeBtnArr;
@property(nonatomic, strong) NSMutableArray *weekBtnArr;
@end

@implementation HealthTimeSetViewController {
    UIDatePicker *startPicker;
    UIDatePicker *endPicker;
    UILabel      *lable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorHEX(0xf0f2f5);
    
    _timeBtnArr = [NSMutableArray array];
    _weekBtnArr = [NSMutableArray array];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    lable               = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    lable.textColor     = UIColorHEX(0x161616);
    lable.font          = SNFONTREGULAR(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text          = _type == 0 ? SN_Text(@"喝水提醒") : SN_Text(@"久坐提醒");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UIButton *rightButton = [[UIButton alloc] init];
    rightButton.frame = CGRectMake(HHHWIDTH-80-10, 20+FitSimpleIpx(0, 20), 80, 44);
    [rightButton setTitleNormal:HHH_Text(@"保存")];
    rightButton.titleLabel.font = SNFONTMEDIUM(16);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton setTitleColorNormal:UIColorHEX(0xFF9500)];
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 这里是用大写的 H
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    NSDate   *date    = [formatter dateFromString:dateStr];
    CGFloat widthPick = 0;
    if (IOS88884444) {
        widthPick = - HHHWIDTH/4;
    };
    startPicker                 = [[UIDatePicker alloc] initWithFrame:CGRectMake(widthPick, naView.bottom+2+FitH(44), HHHWIDTH/2, FitH(150))];
    startPicker.datePickerMode  = UIDatePickerModeTime;
    startPicker.backgroundColor = [UIColor clearColor];
    startPicker.tag             = 100;
    [startPicker setDate:date];
    //    startPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
    startPicker.locale = [NSLocale currentLocale];
    [startPicker addTarget:self
                    action:@selector(pickerChanged:)
          forControlEvents:UIControlEventValueChanged];
    [startPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
    [self.view addSubview:startPicker];
    
    NSArray *arr1 = @[@"", @"-", @""];
    for (int i = 0; i < arr1.count; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FitW(20), FitW(20))];
        label.center    = CGPointMake(HHHWIDTH/4*(i+1), startPicker.centerY);
        label.text      = arr1[i];
        label.textColor = SNCOLORBLACK;
        [self.view addSubview:label];
    }
    
    UIImageView *imageL = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 3, startPicker.height/3-3)];
    imageL.center = CGPointMake(1, startPicker.centerY);
    [imageL setImage:[UIImage imageWithColor:[UIColor blackColor]]];
    [self.view addSubview:imageL];
    
    UIImageView *imageR = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 3, startPicker.height/3-3)];
    imageR.center = CGPointMake(HHHWIDTH-1, startPicker.centerY);
    [imageR setImage:[UIImage imageWithColor:[UIColor blackColor]]];
    [self.view addSubview:imageR];
    
    endPicker                 = [[UIDatePicker alloc] initWithFrame:CGRectMake( widthPick+HHHWIDTH/2, naView.bottom+2+FitH(44), HHHWIDTH/2, FitH(150))];
    endPicker.datePickerMode  = UIDatePickerModeTime;
    endPicker.backgroundColor = [UIColor clearColor];
    endPicker.tag             = 101;
    [endPicker setDate:date];
    //    endPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
    endPicker.locale = [NSLocale currentLocale];
    [endPicker addTarget:self
                  action:@selector(pickerChanged:)
        forControlEvents:UIControlEventValueChanged];
    [endPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
    [self.view addSubview:endPicker];
    
    NSString *tmpStr = [dateStr substringWithRange:NSMakeRange(0, 11)];
    if (_type == 0) {
        NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.drinkStartHour1, SN_USER.drinkStartMins1];
        NSString *endStr   = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.drinkEndHour1, SN_USER.drinkEndMins1];
        startPicker.date      = [NSDate dateWithDate:[formatter dateFromString:startStr]];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDate *now = [NSDate date];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
        NSDate *startDate = [calendar dateFromComponents:components];
        startPicker.maximumDate = [startDate dateByAddingMinutes:1409];
        endPicker.date        = [NSDate dateWithDate:[formatter dateFromString:endStr]];
        endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
    } else {
        NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.sedentaryStartHour1, SN_USER.sedentaryStartMins1];
        NSString *endStr   = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.sedentaryEndHour1, SN_USER.sedentaryEndMins1];
        startPicker.date      = [NSDate dateWithDate:[formatter dateFromString:startStr]];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDate *now = [NSDate date];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
        NSDate *startDate = [calendar dateFromComponents:components];
        startPicker.maximumDate = [startDate dateByAddingMinutes:1409];
        endPicker.date        = [NSDate dateWithDate:[formatter dateFromString:endStr]];
        endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
    }
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, startPicker.bottom+FitH(12), FitW(100), FitH(26))];
    timeLabel.textColor     = SNCOLORBLACK;
    timeLabel.text          = HHH_Text(@"时间间隔");
    timeLabel.adjustsFontSizeToFitWidth = YES;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font          = [UIFont systemFontOfSize:FitFont(17)];
    [self.view addSubview:timeLabel];

    NSArray *arr  = @[@"半小时", @"1小时", @"2小时", @"3小时"];
    CGFloat width = (HHHWIDTH-timeLabel.right-FitW(10))/arr.count;
    for (int i = 0; i < arr.count; i++) {
        UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.frame = CGRectMake(timeLabel.right+FitW(5)+width*i, startPicker.bottom+FitH(10), width, FitH(30));
        timeBtn.tag   = 100+i;
        timeBtn.enabled = NO;
        [timeBtn setTitleColorNormal:SNCOLORBLACK];
        [timeBtn setTitleNormal:SN_Text(arr[i])];
        [timeBtn setImageNormal:@"未选中"];
        [timeBtn setImageSelecte:@"选中1"];
        timeBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(16)];
        [timeBtn addTouchUpTarget:self action:@selector(timeBtnClick:)];
        [self.view addSubview:timeBtn];
        [_timeBtnArr addObject:timeBtn];
       
        if (_type == 0) {
            if (timeBtn.tag == 100+(SN_USER.drinkinterval/30-1)) {
                timeBtn.selected = YES;
            }
        } else {
            if (timeBtn.tag == 100+(SN_USER.sedentaryinterval/30-1)) {
                timeBtn.selected = YES;
            }
        }
    }
    [self changeTimeBtnEnabled];
    NSArray *weekArr = @[HHH_Text(@"星期天"), HHH_Text(@"星期一"), HHH_Text(@"星期二"), HHH_Text(@"星期三"), HHH_Text(@"星期四"), HHH_Text(@"星期五"), HHH_Text(@"星期六")];
    CGFloat height   = (HHHHEIGHT-timeLabel.bottom)/9;
    
    
    for (int i = 0; i <weekArr.count; i++) {
        UIView *bgView = [[UIView alloc] initWithFrame:SNFRAME(0, timeLabel.bottom+20 + i*50, screenWidth, 50)];
        [self.view addSubview:bgView];
        
        UILabel *weekLabel = [[UILabel alloc]initWithFrame:SNFRAME(25, 15, 100, 20)];
        weekLabel.text      = weekArr[i];
        weekLabel.font      = SNFONTMIN(15.0);
        weekLabel.textColor = SNCOLORBLACK;
        [bgView addSubview:weekLabel];
        
        UIButton *weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        weekBtn.frame = SNFRAME(screenWidth -40-25, 5, 40, 40);
        weekBtn.tag   = 100+i;
        [weekBtn setImageNormal:@"未勾选"];
        [weekBtn setImageSelecte:@"已勾选"];
        [weekBtn addTouchUpTarget:self action:@selector(weekBtnClick:)];
        [bgView addSubview:weekBtn];
        
        UIView *lineV= [[UIView alloc]initWithFrame:SNFRAME(0, 50,screenWidth, 0.5)];
        [bgView addSubview:lineV];
        lineV.backgroundColor = UIColorHEX(0xD9D9DD);
        
        if (_type == 0) {
            switch (weekBtn.tag) {
                case 100:
                    weekBtn.selected = SN_USER.drinkSUN;
                    break;
                case 101:
                    weekBtn.selected = SN_USER.drinkMON;
                    break;
                case 102:
                    weekBtn.selected = SN_USER.drinkTUE;
                    break;
                case 103:
                    weekBtn.selected = SN_USER.drinkWED;
                    break;
                case 104:
                    weekBtn.selected = SN_USER.drinkTHU;
                    break;
                case 105:
                    weekBtn.selected = SN_USER.drinkFRI;
                    break;
                case 106:
                    weekBtn.selected = SN_USER.drinkSAT;
                    break;
                default:
                    break;
            }
        } else {
            switch (weekBtn.tag) {
                case 100:
                    weekBtn.selected = SN_USER.sedentarySUN;
                    break;
                case 101:
                    weekBtn.selected = SN_USER.sedentaryMON;
                    break;
                case 102:
                    weekBtn.selected = SN_USER.sedentaryTUE;
                    break;
                case 103:
                    weekBtn.selected = SN_USER.sedentaryWED;
                    break;
                case 104:
                    weekBtn.selected = SN_USER.sedentaryTHU;
                    break;
                case 105:
                    weekBtn.selected = SN_USER.sedentaryFRI;
                    break;
                case 106:
                    weekBtn.selected = SN_USER.sedentarySAT;
                    break;
                default:
                    break;
            }
        }
    }
    
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
//    [self.view addSubview:shadowLine];
}

- (void)leftbuttonClick {
    SN_USER = [SNUserInfoModel getUserInfoFromDBWithUserID:[KK_LastUser getObjectValue]];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonClick {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"HH:mm"];
    NSString *start = [formatter stringFromDate:startPicker.date];
    NSString *end   = [formatter stringFromDate:endPicker.date];
    
    if (_type == 0) {
//        SN_USER.drinkStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
//        SN_USER.drinkStartMins1 = [start substringWithRange:NSMakeRange(3, 2)].integerValue;
//        SN_USER.drinkEndHour1   = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
//        SN_USER.drinkEndMins1   = [end substringWithRange:NSMakeRange(3, 2)].integerValue;
        SN_USER.drinkStartHour1 = startPicker.date.hour;
        SN_USER.drinkStartMins1 = startPicker.date.minute;
        SN_USER.drinkEndHour1   = endPicker.date.hour;
        SN_USER.drinkEndMins1   = endPicker.date.minute;
        SN_USER.showDrinkTime = [NSString stringWithFormat:@"(%@-%@)",start,end];
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                SN_USER.drinkOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startDrinkWater];
    } else {
        SN_USER.showSedentaryTime = [NSString stringWithFormat:@"(%@-%@)",start,end];
        SN_USER.sedentaryStartHour1 = startPicker.date.hour;
        SN_USER.sedentaryStartMins1 = startPicker.date.minute;
        SN_USER.sedentaryEndHour1   = endPicker.date.hour;
        SN_USER.sedentaryEndMins1   = endPicker.date.minute;
//        SN_USER.sedentaryStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
//        SN_USER.sedentaryStartMins1 = [start substringWithRange:NSMakeRange(3, 2)].integerValue;
//        SN_USER.sedentaryEndHour1   = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
//        SN_USER.sedentaryEndMins1   = [end substringWithRange:NSMakeRange(3, 2)].integerValue;
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                SN_USER.sedentaryOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startsedentary];
    }
    [SN_USER updateToDB];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setType:(NSInteger)type {
    _type = type;
}

- (void)timeBtnClick:(UIButton *)sender {
    if (!sender.selected) {
        sender.selected = YES;
        for (UIButton *btn in _timeBtnArr) {
            if (btn.tag != sender.tag) {
                btn.selected = NO;
            }
        }
        if (_type == 0) {
            SN_USER.drinkinterval = 30*(sender.tag-100+1);
        } else {
            SN_USER.sedentaryinterval = 30*(sender.tag-100+1);
        }
    }
}
-(void)changeTimeBtnEnabled{
    NSInteger min = [endPicker.date timeIntervalSinceDate:startPicker.date]/60;
    for (int i=0; i<_timeBtnArr.count; i++) {
        UIButton *btn = _timeBtnArr[i];
        if (min>=30) {
            btn.enabled = i<1?YES:NO;
        }
        if (min>=60) {
            btn.enabled = i<2?YES:NO;
        }
        if (min>=120) {
            btn.enabled = i<3?YES:NO;
        }
        if (min>=180) {
            btn.enabled = i<4?YES:NO;
        }
    }
}
-(void)changeTimeBtnSelected{
    NSInteger min = [endPicker.date timeIntervalSinceDate:startPicker.date]/60;
    for (int i=0; i<_timeBtnArr.count; i++) {
        UIButton *btn = _timeBtnArr[i];
        if (i==3 && btn.selected ) {
            if (min<180) {
                [self timeBtnClick:_timeBtnArr[2]];
            }
            if (min<120) {
                [self timeBtnClick:_timeBtnArr[1]];
            }
            if (min<60) {
                [self timeBtnClick:_timeBtnArr[0]];
            }
        }
        if (i==2 && btn.selected) {
            if (min<120) {
                [self timeBtnClick:_timeBtnArr[1]];
            }
            if (min<60) {
                [self timeBtnClick:_timeBtnArr[0]];
            }
        }
        if (i==1 && btn.selected) {
            if (min<60) {
                [self timeBtnClick:_timeBtnArr[0]];
            }
        }
    }
}
- (void)pickerChanged:(UIDatePicker *)picker {
    endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
    [self changeTimeBtnEnabled];
    [self changeTimeBtnSelected];
}

- (void)weekBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [_weekBtnArr addObject:sender];
    } else {
        [_weekBtnArr removeObject:sender];
    }
    if (_type == 0) {
        if (sender.selected) {
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = YES;
                    break;
                case 101:
                    SN_USER.drinkMON = YES;
                    break;
                case 102:
                    SN_USER.drinkTUE = YES;
                    break;
                case 103:
                    SN_USER.drinkWED = YES;
                    break;
                case 104:
                    SN_USER.drinkTHU = YES;
                    break;
                case 105:
                    SN_USER.drinkFRI = YES;
                    break;
                case 106:
                    SN_USER.drinkSAT = YES;
                    break;
                default:
                    break;
            }
        } else {
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = NO;
                    break;
                case 101:
                    SN_USER.drinkMON = NO;
                    break;
                case 102:
                    SN_USER.drinkTUE = NO;
                    break;
                case 103:
                    SN_USER.drinkWED = NO;
                    break;
                case 104:
                    SN_USER.drinkTHU = NO;
                    break;
                case 105:
                    SN_USER.drinkFRI = NO;
                    break;
                case 106:
                    SN_USER.drinkSAT = NO;
                    break;
                default:
                    break;
            }
        }
    } else {
        if (sender.selected) {
            switch (sender.tag) {
                case 100:
                    SN_USER.sedentarySUN = YES;
                    break;
                case 101:
                    SN_USER.sedentaryMON = YES;
                    break;
                case 102:
                    SN_USER.sedentaryTUE = YES;
                    break;
                case 103:
                    SN_USER.sedentaryWED = YES;
                    break;
                case 104:
                    SN_USER.sedentaryTHU = YES;
                    break;
                case 105:
                    SN_USER.sedentaryFRI = YES;
                    break;
                case 106:
                    SN_USER.sedentarySAT = YES;
                    break;
                default:
                    break;
            }
        } else {
            switch (sender.tag) {
                case 100:
                    SN_USER.sedentarySUN = NO;
                    break;
                case 101:
                    SN_USER.sedentaryMON = NO;
                    break;
                case 102:
                    SN_USER.sedentaryTUE = NO;
                    break;
                case 103:
                    SN_USER.sedentaryWED = NO;
                    break;
                case 104:
                    SN_USER.sedentaryTHU = NO;
                    break;
                case 105:
                    SN_USER.sedentaryFRI = NO;
                    break;
                case 106:
                    SN_USER.sedentarySAT = NO;
                    break;
                default:
                    break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

